package com.moore.service.Impl;

import com.moore.bean.Community;
import com.moore.dto.ResponseData;
import com.moore.mapper.CommunityMapper;
import com.moore.service.CommunityService;
import com.moore.service.EntitiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.moore.utils.Logger.toLog;

@Service
public class CommunityServiceImpl implements CommunityService {
    @Autowired
    private CommunityMapper communityMapper;
    @Autowired
    private EntitiesService entitiesService;

    @Override
    public ResponseData addCommunity(Community community) {
        int result = communityMapper.addCommunity(community);
        if(result == 1){
            toLog("添加小区成功");
            return new ResponseData(200,"添加成功",null,0);
        } else {
            toLog("添加小区失败");
            return new ResponseData(500,"添加失败",null,0);
        }
    }

    @Override
    public ResponseData getCommunity(Community community, HttpServletRequest request) {
        if (community.getSize() == 0){
            community.setSize(10);
        }
        List<Community> communities = communityMapper.getCommunity(community);
        String serverUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
        for (Community value : communities) {
            String relativePath = value.getImage();
            String fullPath = serverUrl + relativePath;
            value.setImage(fullPath);
        }
        toLog("查询小区成功");
        return new ResponseData(200, "查询成功", communities, communityMapper.getCommunityCount(community));
    }

    @Override
    public ResponseData updateCommunity(Community community) {
        int result = communityMapper.updateCommunity(community);
        if(result == 1){
            toLog("修改小区成功");
            return new ResponseData(200,"修改成功",null,0);
        } else {
            toLog("修改小区失败");
            return new ResponseData(500,"修改失败",null,0);
        }
    }

    @Override
    public ResponseData deleteCommunity(List<Community> communities) {
        return entitiesService.deleteEntities(communities, "community_name","已删除");
    }
}
